package bdcin.servlet.actions;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bdcin.classesbasicas.Usuario;


public class Controlador extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	private static final long serialVersionUID = 1L;

	public Controlador() {
		super();
	}   	


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processar(request, response);
	}  	


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.processar(request, response);
	}

	private void processar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		String caminho = "bdcin.servlet.actions."+action;
		try {
			Action act = (Action) Class.forName(caminho).newInstance();	

			Usuario usuario = (Usuario) request.getSession(true).getAttribute("usuarioLogado");

			if (action.equals("LogarAction") || action.equals("documento.BuscarDocumentoAction")  || usuario != null 
					|| action.equals("usuarioDoSistema.EsqueciSenhaAction") || action.equals("EnviarEmailAction")
					|| action.equals("mineracao.BaixarDocumentoAction") || 	action.equals("documento.ProximaPaginaDocumentoAction")	 

			) {
				act.executar(request,response);				
			} else {
				RequestDispatcher rd = null;
				rd = request.getRequestDispatcher("telaLogin.jsp");
				rd.forward(request, response);	
			}
		} catch(Exception ex){
			System.out.println("Pau no Controlador " + ex.getMessage() + "\n");
			ex.printStackTrace();			
		}
	}
}



